<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form>
        <div>
            用户名： <input type="text" name="userName" id="">
        </div>
        <div>
            密码： <input type="text" name="userPwd" id="">
        </div>
        <button type="submit">注册</button>
    </form>

    <script>
        const form = document.forms[0];
        form.onsubmit = function (e) {
            // const userName = form.userName.value;
            // const userPwd = form.userPwd.value;
            e.preventDefault();
            const xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function () {
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        if (xhr.response.flag) {
                            alert(xhr.response.msg)
                            location.assign('login.html')
                        } else {
                            alert(xhr.response.msg)
                            form.userName.focus()
                        }
                    } else {
                        alert('服务器繁忙')
                    }
                }
            }
            xhr.open('post', '/admin/regin', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.responseType = 'json';
            xhr.send(JSON.stringify({
                userName: form.userName.value,
                userPwd: form.userPwd.value
            }))
            // xhr.send(JSON.stringify({ userName, userPwd }));

        }
    </script>
</body>

</html>